﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Application.Admin.Sight
{
    public class OpenSightService : IOpenSightService
    {
        Application.Admin.Sight.ISightPointService sightService;

        Application.Admin.Sight.ISightPointAreaService sightAreaService;

        Application.Admin.Sight.ISightPointClassService sightClassService;

        Application.Admin.Sight.ISightPointCommService sightCommService;

        Application.Admin.Sight.ISightPointUserSiginService sightSiginService;

        Application.Admin.Sight.ISightPointPicService sightPicService;

        Application.Admin.Pic.IOpenPicService openPicService;

        public OpenSightService(Application.Admin.Sight.ISightPointService sight,
            Application.Admin.Sight.ISightPointAreaService sightArea,
            Application.Admin.Sight.ISightPointClassService sightClass,
            Application.Admin.Sight.ISightPointCommService comm,
            Application.Admin.Sight.ISightPointUserSiginService sigin,
            Application.Admin.Sight.ISightPointPicService sightPic,
            Application.Admin.Pic.IOpenPicService openPic)
        {
            if (sight == null)
            {
                throw new ArgumentNullException("sightService is null");
            }
            if (sightArea == null)
            {
                throw new ArgumentNullException("sightAreaService is null");
            }
            if (sightClass == null)
            {
                throw new ArgumentNullException("sightClassService is null");
            }
            if (comm == null)
            {
                throw new ArgumentNullException("commService is null");
            }
            if (sigin == null)
            {
                throw new ArgumentNullException("siginService is null");
            }
            if (sightPic == null)
            {
                throw new ArgumentNullException("sightPicService is null");
            }
            if (openPic == null)
            {
                throw new ArgumentNullException("openPicService is null");
            }
            sightService = sight;
            sightAreaService = sightArea;
            sightClassService = sightClass;
            sightCommService = comm;
            sightSiginService = sigin;
            sightPicService = sightPic;
            openPicService = openPic;
        }

        public List<Dto.OpenSightAllPicDto> GetAllSightAllOpenPic()
        {
            var data = (from sightpic in sightPicService.GetList()
                        join pic in openPicService.GetAllPic() on sightpic.PicInfoId equals pic.Id
                        orderby sightpic.SightId
                        select new Dto.OpenSightAllPicDto
                        {
                            Id = sightpic.SightId,
                            ImageName = pic.Name
                        });
            return data.ToList();
        }

        public Application.Admin.Dto.OpenSightDto GetOpenSightNoPic(Domain.Entity.SightPoint entity)
        {
            var data = new Application.Admin.Dto.OpenSightDto();
            data.AreaId = entity.AreaId;
            data.ClassId = entity.ClassId;
            data.CommCount = sightCommService.GetList().Where(e => e.SightId == entity.Id).Count();
            data.Id = entity.Id;
            data.InternalId = entity.InternalId;
            data.Lat = entity.Lat;
            data.Lon = entity.Lon;
            data.Name = entity.Name;
            data.Remark = entity.Remark;
            data.SiginCount = sightSiginService.GetList().Where(e => e.SightId == entity.Id).Count();
            //var cla = sightClassService.Get(entity.ClassId);
            //data.ClassName = cla != null ? cla.Name : "";
            //data.IpAddress = entity.IpAddress;
            //data.SortNum = entity.SortNum;
            //data.State = entity.State;
            //var area = sightAreaService.Get(entity.AreaId);
            //data.AreaName = area != null ? area.Name : "";
            data.VouchNum = entity.VouchNum;
            return data;
        }

        public Application.Admin.Dto.OpenSightWithOnePicDto
            GetOpenSightWithOneOpenPic(Domain.Entity.SightPoint entity)
        {
            var data = new Application.Admin.Dto.OpenSightWithOnePicDto();
            data.AreaId = entity.AreaId;
            data.ClassId = entity.ClassId;
            data.CommCount = sightCommService.GetList().Where(e => e.SightId == entity.Id).Count();
            data.Id = entity.Id;
            data.InternalId = entity.InternalId;
            data.Lat = entity.Lat;
            data.Lon = entity.Lon;
            data.Name = entity.Name;
            data.Remark = entity.Remark;
            data.SiginCount = sightSiginService.GetList().Where(e => e.SightId == entity.Id).Count();
            data.VouchNum = entity.VouchNum;
            data.SightPic = this.GetSightLatestOpenPic(entity);
            //var cla = sightClassService.Get(entity.ClassId);
            //data.ClassName = cla != null ? cla.Name : "";
            //data.IpAddress = entity.IpAddress;
            //data.Address = entity.Address;
            //data.AddTime = entity.AddTime;
            //var area = sightAreaService.Get(entity.AreaId);
            //data.AreaName = area != null ? area.Name : "";
            //data.SortNum = entity.SortNum;
            //data.State = entity.State;
            if (data.SightPic != null)
            {
                //data.SightPicThumb = openPicService.GetOpenPicThumbByPicId(data.SightPic.Id);
            }
            return data;
        }

        public List<Dto.OpenPicDto> GetSightOpenPicList(Domain.Entity.SightPoint entity)
        {
            var idList = sightPicService.GetList().Where(e => e.SightId == entity.Id && e.State == true)
                .Select(e => e.PicInfoId).ToList();

            var data = openPicService.GetOpenPicByIdList(idList);
            return data;
        }

        public Dto.OpenPicDto GetSightRandOpenPic(Domain.Entity.SightPoint entity)
        {
            var idList = sightPicService.GetList()
                .Where(e => e.SightId == entity.Id && e.State == true).Select(e => e.PicInfoId).ToList();
            Random randService = new Random();
            var randNum = randService.Next(0, (idList.Count() - 1) > 0 ? (idList.Count() - 1) : 0);
            if (idList.Count > 0)
            {
                var data = openPicService.GetOpenPicById(idList[randNum]);
                return data;
            }
            else
            {
                return null;
            }
        }

        public Dto.OpenPicDto GetSightLatestOpenPic(Domain.Entity.SightPoint entity)
        {
            var all = sightPicService.GetList()
                .Where(e => e.SightId == entity.Id && e.State == true);
            var picId = 0;
            if (all.Any())
            {
                picId = all.Max(e => e.PicInfoId);
            }
            var data = openPicService.GetOpenPicById(picId);
            return data;
        }

        public Dto.OpenPicDto GetSightSortNumOpenPic(Domain.Entity.SightPoint entity)
        {
            var picId = sightPicService.GetList()
                .Where(e => e.SightId == entity.Id && e.State == true)
                .OrderBy(e=> e.SortNum).Select(e=> e.PicInfoId).FirstOrDefault();
            var data = openPicService.GetOpenPicById(picId);
            return data;
        }

        public IQueryable<Dto.OpenSightCommDto> GetSightOpenComm(IQueryable<Domain.Entity.SightPointComm> entity)
        {
            var data = entity.Select(e => new Dto.OpenSightCommDto()
            {
                Id = e.Id,
                AddTime = e.AddTime,
                Contents = e.Contents,
                UserName = e.UserName
            });
            return data;
        }

        public List<Dto.OpenSightDto> GetAllOpenSightNoPic()
        {
            var all = sightService.GetList().Where(e => e.State == true);
            var res = new List<Dto.OpenSightDto>();
            foreach (var item in all)
            {
                var temp = new Application.Admin.Dto.OpenSightDto();
                temp.AreaId = item.AreaId;
                temp.ClassId = item.ClassId;
                temp.CommCount = sightCommService.GetList().Where(e => e.SightId == item.Id).Count();
                temp.Id = item.Id;
                temp.InternalId = item.InternalId;
                temp.Lat = item.Lat;
                temp.Lon = item.Lon;
                temp.Name = item.Name;
                temp.Remark = item.Remark;
                temp.SiginCount = sightSiginService.GetList().Where(e => e.SightId == item.Id).Count();
                //var cla = sightClassService.Get(entity.ClassId);
                //data.ClassName = cla != null ? cla.Name : "";
                //data.IpAddress = entity.IpAddress;
                //data.SortNum = entity.SortNum;
                //data.State = entity.State;
                //var area = sightAreaService.Get(entity.AreaId);
                //data.AreaName = area != null ? area.Name : "";
                temp.VouchNum = item.VouchNum;
                res.Add(temp);
            }
            return res;
        }

        public List<Dto.OpenSightWithOnePicDto> GetAllOpenSightWithOneOpenPic()
        {
            var all = sightService.GetList().Where(e => e.State == true);
            var res = new List<Dto.OpenSightWithOnePicDto>();
            foreach (var item in all)
            {
                var temp = new Application.Admin.Dto.OpenSightWithOnePicDto();
                temp.AreaId = item.AreaId;
                temp.ClassId = item.ClassId;
                temp.CommCount = sightCommService.GetList().Where(e => e.SightId == item.Id).Count();
                temp.Id = item.Id;
                temp.InternalId = item.InternalId;
                temp.Lat = item.Lat;
                temp.Lon = item.Lon;
                temp.Name = item.Name;
                temp.Remark = item.Remark;
                temp.SiginCount = sightSiginService.GetList().Where(e => e.SightId == item.Id).Count();
                temp.VouchNum = item.VouchNum;
                temp.SightPic = this.GetSightLatestOpenPic(item);
                //var cla = sightClassService.Get(entity.ClassId);
                //data.ClassName = cla != null ? cla.Name : "";
                //data.IpAddress = entity.IpAddress;
                //data.Address = entity.Address;
                //data.AddTime = entity.AddTime;
                //var area = sightAreaService.Get(entity.AreaId);
                //data.AreaName = area != null ? area.Name : "";
                //data.SortNum = entity.SortNum;
                //data.State = entity.State;
                if (temp.SightPic != null)
                {
                    //data.SightPicThumb = openPicService.GetOpenPicThumbByPicId(data.SightPic.Id);
                }
                res.Add(temp);
            }
            return res;
        }

        public List<Dto.OpenSightWithOnePicDto> GetAllOpenSightWithOneOpenPicBySortNum()
        {
            var all = sightService.GetList().Where(e => e.State == true);
            var res = new List<Dto.OpenSightWithOnePicDto>();
            foreach (var item in all)
            {
                var temp = new Application.Admin.Dto.OpenSightWithOnePicDto();
                temp.AreaId = item.AreaId;
                temp.ClassId = item.ClassId;
                temp.CommCount = sightCommService.GetList().Where(e => e.SightId == item.Id).Count();
                temp.Id = item.Id;
                temp.InternalId = item.InternalId;
                temp.Lat = item.Lat;
                temp.Lon = item.Lon;
                temp.Name = item.Name;
                temp.Remark = item.Remark;
                temp.SiginCount = sightSiginService.GetList().Where(e => e.SightId == item.Id).Count();
                temp.VouchNum = item.VouchNum;
                temp.SightPic = this.GetSightSortNumOpenPic(item);
                //var cla = sightClassService.Get(entity.ClassId);
                //data.ClassName = cla != null ? cla.Name : "";
                //data.IpAddress = entity.IpAddress;
                //data.Address = entity.Address;
                //data.AddTime = entity.AddTime;
                //var area = sightAreaService.Get(entity.AreaId);
                //data.AreaName = area != null ? area.Name : "";
                //data.SortNum = entity.SortNum;
                //data.State = entity.State;
                if (temp.SightPic != null)
                {
                    //data.SightPicThumb = openPicService.GetOpenPicThumbByPicId(data.SightPic.Id);
                }
                res.Add(temp);
            }
            return res;
        }
    }
}
